const db = require('../db/index')
// 获取所有用户的报名信息
exports.getuserinfolist = (req, res) => {
    const userinterview = `select * from interview_status`
    const userinco = `select * from user_basic`

    db.query(userinterview, (err, interviewResults) => {
        if (err) {
            return res.cc(err);
        }

        db.query(userinco, (err, incoResults) => {
            if (err) {
                return res.cc(err);
            }

            incoResults.forEach(item => {
                delete item.password
                interviewResults.forEach(option => {
                    if (option.phone_number === item.phone_number) {
                        Object.assign(item, option);
                    }
                });
            });
            // console.log(incoResults);


            return res.send({ code: 200, msg: 'getuserinfolist', data: incoResults });
        });
    });
}

// 封装数据库查询函数
function queryDatabase(sql, params, callback) {
    db.query(sql, params, function (err, results) {
        if (err) {
            return callback(err, null);
        }
        callback(null, results);
    });
}
// 根据手机号获取用户信息
exports.getuserinfoformphone = (req, res) => {
    const userInfoSql = `select * from user_basic where phone_number=?`;
    // const statusSql = `select * from interview_status where phone_number=?`;
    const statusSql = `select * from interview_status where phone_number=?`;

    // 查询用户基本信息
    queryDatabase(userInfoSql, req.body.phone_number, (err, userInfoResults) => {

        if (err) {
            return res.cc(err);
        }

        if (userInfoResults.length !== 1) {
            return res.cc('获取用户信息失败！');
        }

        const userInfo = userInfoResults[0];
        delete userInfo.password;
        delete userInfo.user_basic_ID;
        // 查询用户状态信息
        queryDatabase(statusSql, req.body.phone_number, (err, statusResults) => {
            if (err) {
                return res.cc(err);
            }


            if (statusResults.length !== 1) {
                const userInfoObj = { ...userInfo };
                res.send({
                    status: 0,
                    message: '获取用户信息成功！未报名',
                    data: userInfoObj,
                });
            } else {


                const statusInfo = statusResults[0];
                delete statusInfo.id;

                // 将用户基本信息和状态信息合并后响应给客户端
                const userInfoObj = { ...userInfo, ...statusInfo };
                res.send({
                    status: 0,
                    message: '获取用户信息成功！已报名',
                    data: userInfoObj,
                });
            }

        });
    });
}



// 获取用户信息
// exports.getUserInfo = (req, res) => {
//     const userInfoSql = `select * from user_basic where id=?`;
//     const statusSql = `select * from interview_status where phone_number=?`;

//     // 查询用户基本信息
//     queryDatabase(userInfoSql, req.user.id, (err, userInfoResults) => {

//         console.log(req.user);
//         if (err) {
//             return res.cc(err);
//         }

//         if (userInfoResults.length !== 1) {
//             return res.cc('获取用户信息失败！');
//         }

//         const userInfo = userInfoResults[0];
//         delete userInfo.password;
//         delete userInfo.user_basic_ID;
//         // 查询用户状态信息
//         queryDatabase(statusSql, req.user.phone_number, (err, statusResults) => {
//             if (err) {
//                 return res.cc(err);
//             }

//             if (statusResults.length !== 1) {
//                 const userInfoObj = { ...userInfo };
//                 res.send({
//                     status: 0,
//                     message: '获取用户信息成功！未报名',
//                     data: userInfoObj,
//                 });
//             } else {
//                 const statusInfo = statusResults[0];
//                 delete statusInfo.id;
//                 // 将用户基本信息和状态信息合并后响应给客户端
//                 const userInfoObj = { ...userInfo, ...statusInfo };
//                 res.send({
//                     status: 0,
//                     message: '获取用户信息成功！已报名',
//                     data: userInfoObj,
//                 });
//             }


//         });
//     });
// };


// 修改报名信息
exports.edituserinfolist = (req, res) => {
    // console.log(req.user.id);
    return res.send({ code: 200, msg: 'edituserinfolist' })
}
// 删除报名信息
exports.deleteuserinfolist = (req, res) => {
    // console.log(req.user.id);
    return res.send({ code: 200, msg: 'deleteuserinfolist' })
}